package user

import (
	"ser1/database"
	"time"

	"gorm.io/gorm"
)

type User struct {
	gorm.Model

	Username   string    `json:"username" gorm:"column:username;size:50;not null;index:,unique" example:"admin"`
	SecondName string    `json:"second_name" gorm:"column:second_name;size:50;not null" example:"admin"`
	Password   string    `json:"password" gorm:"column:password;size:255;not null" example:"123456"`
	DepartId   uint      `json:"depart_id" gorm:"column:depart_id;not null;index" example:"1"`
	Phone      string    `json:"phone" gorm:"column:phone;size:15;not null;index:,unique" example:"1234567890"`
	Email      string    `json:"email" gorm:"column:email;size:255;not null;index:,unique" example:"admin@example.com"`
	Address    string    `json:"address" gorm:"column:address;size:255;not null" example:"123 Main St"`
	Gender     string    `json:"gender" gorm:"column:gender;size:10;not null" example:"Male"`
	Birthday   time.Time `json:"birthday" gorm:"column:birthday;not null" example:"1990-01-01"`
	Token      string    `json:"token" gorm:"column:token;size:255;not null;index" example:"jwt_token"`
	Expire     time.Time `json:"expire" gorm:"column:expire;not null" example:"2023-01-01"`
	Status     int       `json:"status" gorm:"column:status;not null;index" example:"1"`
}

type UserMeta struct {
	gorm.Model

	UserId uint   `json:"user_id" gorm:"column:user_id;not null" example:"1"`
	Key    string `json:"key" gorm:"column:key;size:50;not null" example:"key"`
	Value  string `json:"value" gorm:"column:value;type:text;not null" example:"value"`
}

func DB() *gorm.DB {
	return database.GetDB()
}

// func init() {
// 	database.AutoMigrateTables(&User{}, &UserMeta{})
// 	// 如果没有用户,初始化一个用户
// 	if DB().Migrator().HasTable(&User{}) {
// 		var count int64
// 		DB().Model(&User{}).Count(&count)
// 		if count == 0 {
// 			DB().Create(&User{
// 				Username:   "admin",
// 				SecondName: "admin",
// 				Password:   utils.CreateSecret("123456", getPASS_SECRET()),
// 				DepartId:   1,
// 				Phone:      "1234567890",
// 				Email:      "admin@example.com",
// 				Status:     1,
// 			})
// 		}
// 	}
// }
